package com.symbio.ishenapi.background.controller;

import com.symbio.ishenapi.background.bean.BaseTmPermissionVo;
import com.symbio.ishenapi.background.bean.BaseTmPermissionVoThree;
import com.symbio.ishenapi.background.bean.UserInfo;
import com.symbio.ishenapi.background.model.*;
import com.symbio.ishenapi.background.sms.SendTelMsgUtils;
import com.symbio.ishenapi.common.bean.LoginUser;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.common.kit.SecurityKit;
import io.jboot.Jboot;
import io.jboot.component.redis.JbootRedis;
import io.jboot.web.controller.annotation.RequestMapping;
import org.apache.commons.codec.digest.DigestUtils;

import java.math.BigInteger;
import java.util.*;

/**
 * wy:用户管理
 */
@RequestMapping("/TmUserManage")
public class TmUserManageController extends BaseController {

    /**
     * wy:用户管理
     * 分页查询所有用户信息
     */
    public void findAllUser(){
        String id = getPara("id");
        String userName = getPara("userEmail");
        String userEmail = getPara("userEmail");
        String userSex = getPara("userSex");
        TmDoctorUser tpu = new TmDoctorUser();
        if (id != null && !"".equals(id)){
            tpu.setId(new BigInteger(id));
        }
        if (userName != null && !"".equals(userName)){
            tpu.setUserNickName(userName.trim());
        }
        if (userEmail != null && !"".equals(userEmail)){
            tpu.setUserEmail(userEmail.trim());
        }
        if (userSex != null && !"".equals(userSex)){
            tpu.setUserSex(userSex.trim());
        }
        Map map = TmDoctorUser.dao.findAllUser(getQueryModel(),tpu);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * wy:角色管理
     * wy: 角色添加用户
     */
    public void roleAddUser(){
        TmUserRole tur = (TmUserRole)getReqBodyObject(TmUserRole.class);
        tur.setCreateTime(new Date());
        tur.setStatus("1");
        if (tur.save()){
            renderJsonRet(50001);
        }else {
            renderJsonError(50002,"添加");
        }
    }

    /**
     * wy:用户管理
     * 查询一个用户信息
     */
    public void findOneUser(){
        String id = getPara("id");
        if (id==null && "".equals(id)){
            renderJsonError(40003,"用户信息");
            return;
        }
        renderJsonRet(TmDoctorUser.dao.findById(id));
    }

    /**
     *wy:用户管理
     *   修改删除用户
     */
    public void updateUser(){
        TmDoctorUser tdu = (TmDoctorUser)getReqBodyObject(TmDoctorUser.class);
        if (tdu.getId()==null && "".equals(tdu.getId())){
            renderJsonError(40003,"用户信息");
            return;
        }
        try {
            //保存用户
            if (tdu.update()){
                renderJsonRet(50001);
            }else {
                renderJsonError(50002,"修改");
                return;
            }
        }catch (Exception e){
            renderJsonError(50002,"修改");
            e.printStackTrace();
        }
    }

    //wy:角色添加用户方法
    public boolean addUserRole(TmDoctorUser tdu){
        String[] split = tdu.getRoleIds().split(",");
        boolean flag = false;
        try {
            for (int i = 0; i < split.length; i++){
                TmUserRole tur = new TmUserRole();
                tur.setCreateTime(new Date());
                tur.setUserId(tdu.getId());
                tur.setRoleId(new BigInteger(split[i]));
                tur.setStatus("1");
                flag = true;
            }
        } catch (Exception e){
            flag = flag;
            e.printStackTrace();
        }
        return flag;
    }
    /**
     *wy:用户管理
     *   添加用户
     *  / 后台没有添加用户 /
     */
    /*public void addUser(){
        TmDoctorUser tdu = (TmDoctorUser)getReqBodyObject(TmDoctorUser.class);
        if (tdu.getLogName()==null && "".equals(tdu.getLogName())){
            renderJsonError(40003,"用户名");
            return;
        }
        if (tdu.getUserMobilePhone()==null && "".equals(tdu.getUserMobilePhone())){
            renderJsonError(40003,"手机号");
            return;
        }
        try {
            //角色添加用户
            if (tdu.getStatus() == null && "".equals(tdu.getStatus())) {
                addUserRole(tdu);
            }
            //添加用户
            tdu.setCreateTtime(new Date());
            tdu.setStatus(1);
            if (tdu.save()){
                renderJsonRet(tdu);
            }else {
                renderJsonError(50002,"修改");
                return;
            }
        }catch (Exception e){
            renderJsonError(50002,"修改");
            e.printStackTrace();
        }
    }*/

    /**
     * 用户管理
     * wy:批量禁用用户
     */
    public void delInformed(){
        String ids = getReqBodyString().replace("\"","").replace("\"","");
        if (TmDoctorUser.dao.delInformed(ids)) {
            renderJsonRet(50001);
        }else {
            renderJsonError(50002,"禁用");
        }
    }

    /**
     * 用户管理
     * wy:重置密码
     */
    public void resetPassword(){
        TmDoctorUser tdu = (TmDoctorUser)getReqBodyObject(TmDoctorUser.class);
        if (tdu.getId()!=null && !"".equals(tdu.getId())){
            String randNum = SendTelMsgUtils.createRandNum();
            String password = DigestUtils.md5Hex(randNum);
            tdu.setUserPassWork(password);
            tdu.update();
            renderJsonRet(randNum);
            System.out.println("密码:"+randNum+"---MD5后:"+password);
        }else {
            renderJsonError(50002,"修改");
        }
    }

    /**
     * wy:用户管理
     *     用户登录
     */
    public void userLogin(){
        TmDoctorUser login = (TmDoctorUser)getReqBodyObject(TmDoctorUser.class);
        TmDoctorUser doctorUser = TmDoctorUser.dao.findDoctorUser(login);
        TmDoctorUser doctorUserPer = TmDoctorUser.dao.findDoctorUserPer(login);
        BigInteger id = doctorUser.getId();
        if (doctorUser == null && "".equals(doctorUser)){
            renderJsonError(50005,"用户");
            return;
        }else if (doctorUserPer == null && "".equals(doctorUserPer)) {
            renderJsonError(50006);
            return;
        }else {
            System.out.println(login.getPassword());
            String s = DigestUtils.md5Hex(login.getPassword());
            System.out.println(s);
            String password = doctorUser.getPassword();
            if (s.equals(password)) {
                List<BaseTmPermissionVoThree> permission = findByuserIdAndPermission(id);
                if (permission != null && permission.size() > 0){
                    // 缓存用户信息
                    LoginUser _user_ = new LoginUser();
                    _user_.setId(id.toString());
                    _user_.setUserName(doctorUser.getUserName());
                    _user_.setPhone(doctorUser.getUserMobilePhone());
                    _user_.setPicPath(doctorUser.getUserHeadPortrait());
                    String doctorIds = findIdToType(id);
                    _user_.setDoctorIds(doctorIds);
                    String token = SecurityKit.encrypt(System.currentTimeMillis()+""+doctorUser.getId(),doctorUser.getUserMobilePhone());
                    JbootRedis redis = Jboot.me().getRedis();
                    redis.set(token, _user_);
                    System.out.println("========="+token);
                    Map map = new HashMap();
                    map.put("doctorUser", doctorUser);
                    map.put("permission", permission);
                    map.put("token", token);
                    renderJsonRet(map);
                    return;
                }else{
                    renderJsonError(40003,"用户功能权限");
                    return;
                }

            } else {
                renderJsonError(40018);
                return;
            }
        }
    }

/**
 * =================================================================================================================================================================
 */
    /**
     * wy:角色管理
     *  查询所有角色
     */
    public void findAllRole(){
        TmRoleInfo tri = new TmRoleInfo();
        renderJson(tri.findAll());
    }

    /**
     * wy:角色管理
     *  分页查询所有角色
     */
    public void findAllPageRole(){
        String roleName = getPara("roleName");
        String id = getPara("id");
        Map map = TmRoleInfo.dao.findAllPageRole(getQueryModel(),roleName,id);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * wy:角色管理
     *  查询角色详情
     */
    public void findOneRole(){
        TmRoleInfo tri = (TmRoleInfo)getReqBodyObject(TmRoleInfo.class);
        if (tri.getId()!=null && !"".equals(tri.getId())){
            TmRoleInfo tmRoleInfo = tri.findById(tri.getId());
            renderJsonRet(tmRoleInfo);
        }else {
            renderJsonError(40003,"用户信息");
        }
    }

    /**
     *wy:角色管理
     *   修改删除角色
     */
    public void updateRole(){
        TmRoleInfo tri = (TmRoleInfo)getReqBodyObject(TmRoleInfo.class);
        if (tri.getStatus() == null || "".equals((tri.getStatus()).trim())){
            //tri.update();
            //删除该角色现有权限
            TmRolePermission trp = new TmRolePermission();
            trp.deleteByRoleId(tri.getId());

            //添加角色关联权限
            addRolePermission(tri);
            tri.setPermissionIds(null);
        }

        //角色修改保存
        if (tri.update()){
            renderJsonRet(50001);
            return;
        }else {
            renderJsonError(50002,"修改");
            return;
        }
    }

    /**
     * wy:角色管理
     * wy: 添加角色
     */
    public void saveRole(){
        TmRoleInfo tri = (TmRoleInfo)getReqBodyObject(TmRoleInfo.class);
        String trim = tri.getRoleName().trim();
        if(tri.findByName(trim) || trim == null || "".equals(trim)){
            renderJsonError(40015,"角色名");
            return;
        }
        try {
            tri.setRoleName(trim);
            tri.setCreateTime(new Date());
            tri.setStatus("1");
            //角色权限保存
            if (tri.save()){
                addRolePermission(tri);
                renderJsonRet(50001);
            }else {
                renderJsonError(50002,"保存");
            }
        }catch (Exception e){
            renderJsonError(50002,"保存");
            e.printStackTrace();
        }
    }

    //wy:角色权限保存方法
    public boolean addRolePermission(TmRoleInfo tri){
        String[] split = tri.getPermissionIds().split(",");
        boolean flag = false;
        for (int i = 0; i < split.length; i++){
            TmRolePermission trp = new TmRolePermission();
            trp.setRoleId(tri.getId());
            trp.setCreateTime(new Date());
            trp.setPermissionId(new BigInteger(split[i]));
            trp.setCreateBy(tri.getCreateBy());
            trp.setStatus("1");
            trp.save();
            flag = true;
        }
        return flag;
    }

    /**
     * wy:角色管理
     * wy:批量禁用角色
     */
    public void delRoleInfo(){
        String ids = getReqBodyString().replace("\"","").replace("\"","");
        if (TmRoleInfo.dao.delRoleInfo(ids)) {
            renderJsonRet(50001);
        }else {
            renderJsonError(50002,"禁用");
        }
    }

/**
 * ======================================================================================================================
 */

    /**
     * wy:角色管理
     *  查询所有
     */
    public void findAllPermission(){
        String permissionName = getPara("permissionName");
        String pageTitle = getPara("pageTitle");
        Map map = TmPermissionInfo.dao.findAllPermission(getQueryModel(),permissionName.trim(),pageTitle.trim());
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     *wy:角色管理
     *   修改删除角色
     */
    public void updatePermission(){
        TmPermissionInfo tpi = (TmPermissionInfo)getReqBodyObject(TmPermissionInfo.class);
        try {
            //角色修改保存
            if (tpi.update()){
                renderJsonRet(50001);
            }else {
                renderJsonError(50002,"修改");
            }
        }catch (Exception e){
            renderJsonError(50002,"修改");
            e.printStackTrace();
        }
    }

    /**
     * wy:角色管理
     * wy: 添加角色
     */
    public void savePermission(){
        TmPermissionInfo tpi = (TmPermissionInfo)getReqBodyObject(TmPermissionInfo.class);
        try {
            tpi.setCreateTime(new Date());
            tpi.setStatus("1");
            if (tpi.save()){
                renderJsonRet(50001);
            }else {
                renderJsonError(50002,"保存");
            }
        }catch (Exception e){
            renderJsonError(50002,"保存");
            e.printStackTrace();
        }
    }


    /**
     * wy:角色管理
     * wy:批量禁用角色
     */
    public void delPermission(){
        String ids = getReqBodyString().replace("\"","").replace("\"","");
        if (TmPermissionInfo.dao.delPermission(ids)) {
            renderJsonRet(50001);
        }else {
            renderJsonError(50002,"禁用");
        }
    }

/**
 * ======================================================================================================================
 */

    /**
     * wy:查询所有的权限
     *  根据角色默认选中
     */
    public void findAllPerToTree(){
        String roleId = getPara("roleId");
        List<TmPermissionInfo> findOneList = TmPermissionInfo.dao.findTwo(new BigInteger("1")); //一级权限查询接收

        List<BaseTmPermissionVo> oneList = addList(findOneList,roleId);

        Map maxMap = new HashMap();
        Map map = new HashMap();
        map.put("code",200);
        map.put("message","操作成功");
        maxMap.put("children",oneList);
        maxMap.put("status",map);
        renderJsonRet(maxMap);
    }

    //转换到临时Bean上的方法
    public List<BaseTmPermissionVo> addList(List<TmPermissionInfo> findOneList,String roleId){

        List<BaseTmPermissionVo> oneList = new ArrayList<>();
        List<TmRolePermission> perIdList = new ArrayList<>();
        List list = new ArrayList();

        if (roleId != null && !"".equals(roleId)){
            perIdList = TmRolePermission.dao.findByRoleIdPermission(roleId);
            for (TmRolePermission trp:perIdList) {
                list.add(trp.getPermissionId());
            }
        }

        for (int i = 0; i < findOneList.size(); i++) {
            Boolean checked = false;
            BaseTmPermissionVo tpi = new BaseTmPermissionVo();
            tpi.setId(findOneList.get(i).getId());
            tpi.setLabel(findOneList.get(i).getLabel());
            tpi.setCreateTime(findOneList.get(i).getCreateTime());
            tpi.setCreateBy(findOneList.get(i).getCreateBy());
            tpi.setStatus(findOneList.get(i).getStatus());
            tpi.setIsmean(findOneList.get(i).getIsmean());
            tpi.setSpinner(findOneList.get(i).getSpinner());
            tpi.setIcon(findOneList.get(i).getIcon());
            tpi.setJump(findOneList.get(i).getJump());
            tpi.setPageTitle(findOneList.get(i).getPageTitle());
            tpi.setParentId(findOneList.get(i).getParentId());
            for (int z = 0; z<list.size(); z++){
                if (list.get(z).equals(tpi.getId())){
                    checked = true;
                    break;
                }
            }
            tpi.setChecked(checked);
            oneList.add(tpi);
            //查询二级权限
            List<TmPermissionInfo> findTwoList = TmPermissionInfo.dao.findTwo(tpi.getId());
            List<BaseTmPermissionVo> twoList = new ArrayList<>(); //二级权限接收
            for (int j = 0; j < findTwoList.size(); j++) {
                checked = false;
                //将二级权限添加到Bean中
                BaseTmPermissionVo twotpi = new BaseTmPermissionVo();
                twotpi.setId(findTwoList.get(j).getId());
                twotpi.setLabel(findTwoList.get(j).getLabel());
                twotpi.setCreateTime(findTwoList.get(j).getCreateTime());
                twotpi.setCreateBy(findTwoList.get(j).getCreateBy());
                twotpi.setStatus(findTwoList.get(j).getStatus());
                twotpi.setIsmean(findTwoList.get(j).getIsmean());
                twotpi.setSpinner(findTwoList.get(j).getSpinner());
                twotpi.setIcon(findTwoList.get(j).getIcon());
                twotpi.setJump(findTwoList.get(j).getJump());
                twotpi.setPageTitle(findTwoList.get(j).getPageTitle());
                twotpi.setParentId(findTwoList.get(j).getParentId());
                for (int z = 0; z<list.size(); z++){
                    if (list.get(z).equals(twotpi.getId())){
                        checked = true;
                        break;
                    }
                }
                twotpi.setChecked(checked);
                twoList.add(twotpi);
                oneList.get(i).setChildren(twoList);
            }
        }

        for(int k=0;k<oneList.size();k++){
            List<BaseTmPermissionVo> twoVoList = oneList.get(k).getChildren();//取出二级
            if(twoVoList != null && twoVoList.size() > 0){
                for(int n=0;n<twoVoList.size();n++){
                    List<BaseTmPermissionVo> threeList = new ArrayList<>(); //三级权限接收
                    //查询三级
                    List<TmPermissionInfo> findThreeList = TmPermissionInfo.dao.findTwo(oneList.get(k).getChildren().get(n).getId());

                    for (int m = 0; m < findThreeList.size(); m++) {
                        Boolean checked = false;
                        //将二级权限添加到Bean中
                        BaseTmPermissionVo threetpi = new BaseTmPermissionVo();
                        threetpi.setId(findThreeList.get(m).getId());
                        threetpi.setLabel(findThreeList.get(m).getLabel());
                        threetpi.setCreateTime(findThreeList.get(m).getCreateTime());
                        threetpi.setCreateBy(findThreeList.get(m).getCreateBy());
                        threetpi.setStatus(findThreeList.get(m).getStatus());
                        threetpi.setIsmean(findThreeList.get(m).getIsmean());
                        threetpi.setSpinner(findThreeList.get(m).getSpinner());
                        threetpi.setIcon(findThreeList.get(m).getIcon());
                        threetpi.setJump(findThreeList.get(m).getJump());
                        threetpi.setPageTitle(findThreeList.get(m).getPageTitle());
                        threetpi.setParentId(findThreeList.get(m).getParentId());

                        for (int z = 0; z<list.size(); z++){
                            if (list.get(z).equals(threetpi.getId())){
                                checked = true;
                                break;
                            }
                        }
                        threetpi.setChecked(checked);
                        threeList.add(threetpi);
                        oneList.get(k).getChildren().get(n).setChildren(threeList);
                    }
                }
            }

        }
        return oneList;
    }
    /**
     * wy:用户登录时使用
     * 根据用户ID查询该用户所拥有的一级权限的方法
     */
    public List<BaseTmPermissionVoThree> findByuserIdAndPermission(BigInteger userId){
        String id = getPara("userId");
        BigInteger userIds = null;
        if (id != null && !"".equals(id)){
            userIds = new BigInteger(id);
        }else if (userId != null && !"".equals(userId)){
            userIds = userId;
        }else {
            return null;
        }
        //TmDoctorUser tdu = new TmDoctorUser();
        //查询用户拥有的一级权限
        List<TmPermissionInfo> oneList = TmPermissionInfo.dao.findByIdAndOnePermission(userIds,new BigInteger("1"));
        List<BaseTmPermissionVoThree> tpvList = new ArrayList<>();
        for (int i = 0; i < oneList.size(); i++) {
            BaseTmPermissionVoThree tpi = new BaseTmPermissionVoThree();
            tpi.setId(oneList.get(i).getId());
            tpi.setLabel(oneList.get(i).getLabel());
            tpi.setCreateTime(oneList.get(i).getCreateTime());
            tpi.setCreateBy(oneList.get(i).getCreateBy());
            tpi.setStatus(oneList.get(i).getStatus());
            tpi.setIsmean(oneList.get(i).getIsmean());
            tpi.setSpinner(oneList.get(i).getSpinner());
            tpi.setIcon(oneList.get(i).getIcon());
            tpi.setJump(oneList.get(i).getJump());
            tpi.setPageTitle(oneList.get(i).getPageTitle());
            tpi.setParentId(oneList.get(i).getParentId());
            List<TmPermissionInfo> children = TmPermissionInfo.dao.findByIdAndOnePermission(userIds,oneList.get(i).getId());

            tpi.setList(children);
            tpvList.add(tpi);
        }
        if (id != null && !"".equals(id)){
            renderJsonRet(tpvList);
            return null;
        }else {
            return tpvList;
        }
    }

    /**
     * wy:根据ID查询该ID对应的下一级权限
     */
    public void findByIdAndPermission(){
        String userId = getPara("userId");  //用户id
        String parentId = getPara("parentId");          //上级权限的id
        if (userId != null && !"".equals(userId) && parentId != null && !"".equals(parentId)){
            //根据ID查询该ID对应的下一级权限
            List<TmPermissionInfo> oneList = TmPermissionInfo.dao.findByIdAndPermission(userId,parentId);
            renderJsonRet(oneList);
            return;
        }else {
            renderJsonError(40003,"用户");
            return;
        }
    }

    /**
     * wy:根据角色Id或
     *          角色名查询所拥有的权限
     */
    public void findByRoleNameAndPermission(){
        String id = getPara("id");
        String roleName = getPara("roleName");
        TmRoleInfo tri = new TmRoleInfo();

        //查询用户是否被禁用
        if (!tri.findByRoleNameAndPermission(id,roleName.trim())) {
            renderJsonError(40003, "角色");
            return;
        }
        //查询用户拥有的权限
        Map map = TmPermissionInfo.dao.findByRoleNameAndPermission(getQueryModel(),id,roleName.trim());
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * wy:权限管理
     *      添加权限
     */
    public void addPermission(){
        TmPermissionInfo tpi = (TmPermissionInfo)getReqBodyObject(TmPermissionInfo.class);
        tpi.setStatus("1");
        tpi.setCreateTime(new Date());
        if (tpi.save()){
            renderJsonRet(50001);
        }else {
            renderJsonError(50002,"添加");
        }
    }

    /**
     * wy:权限管理
     * wy: 修改/删除
     */
    public void upPermission(){
        TmPermissionInfo tpi = (TmPermissionInfo)getReqBodyObject(TmPermissionInfo.class);

        if (tpi.getId() != null && !"".equals(tpi.getId())){
            tpi.update();
            renderJsonRet(50001);
        }else {
            renderJsonError(50002,"添加");
        }
    }


    /**
     * wy:用户管理
     * wy: 修改密码
     */
    public void updateUserPassword(){
        TmDoctorUser t = (TmDoctorUser)getReqBodyObject(TmDoctorUser.class);
        if (t.getUserPassWork() == null || "".equals((t.getUserPassWork().trim()))){
            renderJsonError(40003,"密码");
            return;
        }
        TmDoctorUser tdu = TmDoctorUser.dao.findById(t.getId());
        String s = DigestUtils.md5Hex(t.getPassword());
        if (s.equals(tdu.getPassword())){
            tdu.setPassword(DigestUtils.md5Hex(t.getUserPassWork()));
            tdu.update();
            renderJsonRet(50001);
        }else {
            renderJsonError(50002,"原始密码不正确,修改");
        }
    }

    /**
     * wy:用户管理
     *      根据用户ID查询有哪些角色
     */
    public void findByUserIdToRole(){
        TmUserRole tur = (TmUserRole)getReqBodyObject(TmUserRole.class);
        List<TmRoleInfo> list = TmRoleInfo.dao.findByUserIdToRole(tur.getUserId());
        String roleName = "";
        for (int i = 0; i < list.size(); i++){
            roleName += list.get(i).getRoleName();
            if ((i+1) < list.size()){
                roleName += ",";
            }
        }
        renderJsonRet(roleName);
    }

    /**
     *	wy:通用方法
     *  根据用户ID查询角色,
     *  如果当前ID是医生,则返回ID.
     *  如果当前ID是医助,关联查询当前医助所关联的医生id
     *  如果当前ID是管理员,则什么都不返回
     */
    /*public String findIdToDoctorId(BigInteger userId){
     *//*String userIds = getPara("userId");
        BigInteger userId = null;
        if (userIds != null && !"".equals(userIds)){
            userId = new BigInteger(userIds);
        }*//*
        String Ids = "";
        List<TmRoleInfo> list = TmRoleInfo.dao.findByUserIdToRole(userId);
        for (TmRoleInfo tri:list) {
            if (tri.getRoleName() == "医生" || "医生".equals(tri.getRoleName())){
                Ids += userId;
            }else if(tri.getRoleName() == "医助" || "医助".equals(tri.getRoleName())){
                Ids = idToString(Ids, userId);
            }
        }
        *//*if (userIds != null && !"".equals(userIds)) {
            renderJsonRet(Ids);
        }*//*

        return Ids;
    }*/


    /**
     *	wy:医生ID关联查询,
     *  如果是医生,返回医生ID
     *  如果是医助,返回关联的医生Ids
     *  如果是管理员,什么都不返回
     */
    public String findIdToType(BigInteger userId){
        /*String userIds = getPara("userId");
        BigInteger userId = null;
        if (userIds != null && !"".equals(userIds)){
            userId = new BigInteger(userIds);
        }*/
        List<TmRoleidUsertype> list = TmRoleidUsertype.dao.findIdToType(userId);
        String Ids = "";
        if (list != null && list.size() >0) {
            for (TmRoleidUsertype tri : list) {
                if (tri.getType() == 3 || "3".equals(tri.getType().toString())) {
                    List<TmDoctorUser> t = TmDoctorUser.dao.findByUserIdToDoctorId(userId);
                    for (int i = 0; i < t.size(); i++){
                        if (userId != t.get(i).getId() && !userId.equals(t.get(i).getId())){
                            Ids += t.get(i).getId();
                        }
                        if ((i+list.size()) < t.size()) {
                            Ids += ",";
                        }
                    }
                }
                if (tri.getType() == 4 || "4".equals(tri.getType().toString())) {
                    Ids += userId;
                }
                if (tri.getType() == 2 || "2".equals(tri.getType().toString()) || tri.getType() == 1 || "1".equals(tri.getType().toString())){
                    Ids = "";
                    return Ids;
                }
            }
        }
        if (Ids.endsWith(",")) {
            Ids = Ids.substring(0,(Ids.length() - 1));
        }

        return Ids;
    }

    public String findTokenGetDoctorId(/*String token*/){
        //String token = getHeader("X-Token");
        String token = getPara("token");
        JbootRedis redis = Jboot.me().getRedis();
        UserInfo info = redis.get(token);
        String Ids = info.getDoctorIds();

        renderJsonRet(Ids);

        return Ids;
    }
}
